﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.Entity;
using System.IO;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Administrator
{
    public partial class BannerList : BasePage
    {
        private BannerLogic bannerLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowBannerList(1);
            }
        }

        private void ShowBannerList(int pageIndex)
        {
            this.gvBanners.PageIndex = pageIndex;
            var bannerList = bannerLogic.GetList().OrderByDescending(p => p.Order).ToList();
            this.gvBanners.DataSource = bannerList;
            this.gvBanners.DataBind();
        }

        protected override void InitLogic()
        {
            bannerLogic = new BannerLogic();
        }

        protected override void DisposeLogic()
        {
            bannerLogic.Dispose();
            bannerLogic = null;
        }

        private void ClearControls()
        {
            ClearControlText(this.txtId, this.BannerTitle, this.BannerOrder, this.BannerLink);
            this.SavePushButton.CommandArgument = EditStateEnum.New.ToString();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButtonEdit_Click(object sender, EventArgs e)
        {
            LinkButton linkButton = sender as LinkButton;
            if (linkButton != null)
            {
                long id = long.Parse(linkButton.CommandArgument);
                Banner banner = bannerLogic.GetList(p => p.Id == id).FirstOrDefault();
                if (banner != null)
                {
                    this.txtId.Value = banner.Id.ToString();
                    this.BannerTitle.Text = banner.Title;
                    this.BannerLink.Text = banner.Link;
                    this.BannerOrder.Text = banner.Order.ToString();
                    this.SavePushButton.CommandArgument = EditStateEnum.Edit.ToString();
                }
            }
        }

        protected void gvBanners_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ShowBannerList(e.NewPageIndex);
        }

        protected void gvBanners_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int pageIndex = this.gvBanners.PageIndex;
            long id = (int)e.Keys[0];
            var banner = bannerLogic.GetList(p => p.Id == id).FirstOrDefault();
            if (banner != null)
            {
                bannerLogic.Delete(banner);
            }
            bannerLogic.UpdateBannerJs();
            pageIndex = this.gvBanners.PageCount < pageIndex ? this.gvBanners.PageCount : pageIndex;
            ShowBannerList(pageIndex);
        }

        protected void SavePushButton_Click(object sender, EventArgs e)
        {
            try
            {
                Banner banner = GetEditBanner();
                bannerLogic.Save(banner);
                bannerLogic.UpdateBannerJs();              
                ShowBannerList(this.gvBanners.PageIndex); 
                AddScript("alert('保存幻灯片成功！');");
                ClearControls();

            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('保存幻灯片失败！');");
            }
        }
        
        /// <summary>
        /// 获取正在编辑的幻灯图片
        /// </summary>
        /// <returns></returns>
        private Banner GetEditBanner()
        {
            Banner banner = null;

            if (SavePushButton.CommandArgument == EditStateEnum.Edit.ToString())
            {
                banner = bannerLogic.GetList(p => p.Id == long.Parse(txtId.Value)).FirstOrDefault();
            }
            else
            {
                banner = new Banner();
                banner.Id = bannerLogic.GetNewIdentity();
                banner.CreatedTime = DateTime.Now;
            }
            if (banner != null)
            {
                banner.Title = BannerTitle.Text;
                banner.Order = int.Parse(BannerOrder.Text);
                banner.Link = BannerLink.Text;
                UploadImage(banner);
            }
            return banner;
        }

        /// <summary>
        /// 上传图片并赋值
        /// </summary>
        /// <param name="banner"></param>
        private void UploadImage(Banner banner)
        {
            if (BannerImage.HasFile)
            {
                string[] path = UtilityTool.GetNewPicPath(BannerImage.FileName);
                string physicalPath = path[1];
                if (!Directory.Exists(Path.GetDirectoryName(physicalPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(physicalPath));
                }
                BannerImage.PostedFile.SaveAs(physicalPath);
                banner.Image = path[0];
            }
        }

        protected void btnReCreate_Click(object sender, EventArgs e)
        {
            bannerLogic.UpdateBannerJs();
        }
    }
}